package com.example.controller;

import com.example.common.Result;
import com.example.entity.Film;
import com.example.service.FilmService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/film")
public class FilmController {

    @Resource
    private FilmService filmService;

    @GetMapping("/selectPage")
    public Result selectPage(Film film, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "4") Integer pageSize){
        PageInfo<Film> pageInfo = filmService.selectPage(film,pageNum,pageSize);
        return Result.success(pageInfo);
    }

    // 用户推荐
    @GetMapping("/selectByUser/{id}")
    public Result selectByUser(@PathVariable Integer id){
        List<Film> list = filmService.selectByUser(id);
        return Result.success(list);
    }

    // 用户评论
    @GetMapping("/selectByComment/{id}")
    public Result selectByComment(@PathVariable Integer id){
        List<Film> list = filmService.selectByComment(id);
        return Result.success(list);
    }

    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id){
        Film film = filmService.selectById(id);
        return Result.success(film);
    }

    // 获取推荐电影列表
    @GetMapping("/selectRecommend/{id}")
    public Result selectRecommend(@PathVariable Integer id){
        List<Film> list = filmService.selectRecommend(id);
        return Result.success(list);
    }

    @PostMapping("/add")
    public Result add(@RequestBody Film film){
        filmService.add(film);
        return Result.success();
    }

    @PutMapping("/update")
    public Result update(@RequestBody Film film){
        filmService.update(film);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        filmService.delete(id);
        return Result.success();
    }

    @DeleteMapping("/delete-batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        filmService.deleteBatch(ids);
        return Result.success();
    }

    // 获取电影排行榜列表
    @GetMapping("/getFilmRecommend")
    public Result getFilmRecommend() {
        try {
            return Result.success(filmService.getFilmRecommend());
        }catch (Exception e){
            System.out.println("获取推荐列表失败");
            return Result.error();
        }
    }
}
